import SwiftUI


struct AddExpenseView: View {
    @State private var establishmentName: String = ""
    @State private var amount: String = ""
    @State private var category: String = "Food"
    @State private var note: String = ""
    @EnvironmentObject var budgetStore: BudgetStore
    @EnvironmentObject var expenseStore: ExpenseStore



    @State private var showPhotoPicker: Bool = false
    @State private var showSavedMessage: Bool = false

    private let categories = [
        "Food", "Transport", "Shopping", "Bills", "Travel", "Other"
    ]

    var onSave: (Expense) -> Void

    @Environment(\.dismiss) private var dismiss

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Input Options")) {
                    Button {
                        showPhotoPicker = true
                    } label: {
                        Label("Scan Receipt", systemImage: "camera.fill")
                    }
                    .sheet(isPresented: $showPhotoPicker) {
                        ReceiptCaptureView()
                    }
                }

                Section(header: Text("Manual Entry")) {
                    TextField("Establishment Name", text: $establishmentName)

                    TextField("Amount ($)", text: $amount)
                        .keyboardType(.decimalPad)

                    Picker("Category", selection: $category) {
                        ForEach(categories, id: \.self) { Text($0) }
                    }

                    TextField("Note (Optional)", text: $note)
                }

                Section {
                    Button("Save Expense") {
                        guard let amt = Double(amount) else { return }

                        let expense = Expense(
                            id: UUID(),
                            establishment: establishmentName,
                            amount: amt,
                            category: category,
                            note: note
                        )

                        onSave(expense)
                        budgetStore.applyExpense(expense)

                        establishmentName = ""
                        amount = ""
                        category = "Food"
                        note = ""

                        // Show confirmation message
                        showSavedMessage = true

                        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                            showSavedMessage = false
                            dismiss()
                        }
                    }

                    .frame(maxWidth: .infinity, alignment: .center)

                    if showSavedMessage {
                        Text("Expense Saved!")
                            .foregroundColor(.green)
                            .font(.subheadline.bold())
                            .transition(.opacity)
                    }
                }
            }
            .navigationTitle("Add Expense")
        }
    }
}
